﻿using System.Xml;
using System.IO;

namespace Sex
{
    // experimental, contents of list should be written to and read from xml-files, to be able to configure the program without code changes
    // to be integrated in list class
    public class StaticXml
    {
        static XmlWriter GetXMLWriter(string PathToInfoDirectory, string XMLFileName, bool Rebuild)
        {
            string XMLFileNamePath = PathToInfoDirectory + "\\" + XMLFileName;
            if (!Rebuild)
                if (File.Exists(XMLFileNamePath)) return null;
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            xws.IndentChars = "\t";
            xws.NewLineChars = "\n";
            xws.Encoding = System.Text.Encoding.UTF8;
            XmlWriter writer;
            try
            {
                writer = XmlWriter.Create(XMLFileNamePath, xws);
            }
            catch
            {
                return null;
            }
            return writer;
        }
        static void WriteXMLElement(XmlWriter writer, string Key, string Value)
        {
            writer.WriteStartElement("property");
            writer.WriteAttributeString("key", Key);
            writer.WriteAttributeString("value", Value);
            writer.WriteEndElement();
        }
        static bool WriteXMLMyData(string PathToInfoDirectory, string XMLFileName, bool Rebuild)
        {
            XmlWriter writer = GetXMLWriter(PathToInfoDirectory, XMLFileName, Rebuild);
            if (writer == null) return false;

            writer.WriteStartElement("mydata");
            writer.WriteAttributeString("key", "path");
            WriteXMLElement(writer, "Title", "value");
            writer.WriteEndElement(); 
            writer.Close();
            return true;
        }
        public static object GetMyData(string PathToInfoDirectory)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            XmlReader r;
            try
            {
                r = XmlReader.Create(PathToInfoDirectory, settings);
            }
            catch
            {
                return null;
            }
            r.MoveToContent();
            r.ReadStartElement("mydata");
            // myData.Title = r["value"];
            // r.Read();
            // myData.Description = r["value"];
            // r.Read();
            return null;
        }
    }
}